<?php
class ScSm
{
    public function sc($id,$type='news')
    {
        $url = dirname(Yii::app()->BasePath).'/tpl/smxml/smxml.xml';//模板文件
        $html = file_get_contents($url);	//获取内容

        $strs = $this->getArr($id,$type);	//需要替换的内容
        /* 替换 */
        $html = str_replace("[smxml]",$strs,$html);

        $dir = dirname(dirname(Yii::app()->BasePath)).'/index/'.$type.'/sm_'.$type.'_'.$id.'.xml';	//生成目录
        /*生成功能*/
        if(file_put_contents($dir,$html)){
            //成功
            return true;
        }else{
            //失败
            return false;
        }
        return false;
    }

    private function getArr($id,$type)
    {
        $smxml = download::model()->findByPk($id);
        $gborm = strpos($smxml->android_size,'GB') !== false?'':'M';
        $pattern = "/name='(.+)' versionCode/i";
        preg_match_all($pattern,$smxml->premission,$matches);
        $ss = "/versionName='(.+)'/i";
        preg_match_all($ss,$smxml->premission,$mat);
        $p = Yii::app()->params['download']['classpid'];
        $str = '';
        $url = 'https://www.fam365.com/'.$type.'/'.$id.'.html';
        $str.='<webName><![CDATA['.$smxml->title.']]></webName>';
        $str.='<hostName><![CDATA['.$matches[1][0].']]></hostName>';
        $str.='<datalist>';
        $str.='<item>';
        $str.='<url><![CDATA['.$url.']]></url>';
        $str.='<title><![CDATA['.$smxml->seotitle.'_范米市场]]></title>';
        $str.='<summary><![CDATA['.$smxml->seodescription.']]></summary>';
        $str.='<name><![CDATA['.$smxml->title.']]></name>';
        $str.='<publishDate><![CDATA['.date('Y-m-d H:i:s',$smxml->addtime).']]></publishDate>';
        $str.='<icon><![CDATA[https://img.fam365.com/'.$smxml->logo.']]></icon>';
        if ($smxml->pid == 1){
            $str.='<fileSize><![CDATA['.$smxml->android_size.$gborm.']]></fileSize>';
            $str.='<version><![CDATA['.$mat[1][0].']]></version>';
            $str.='<os><![CDATA[android]]></os>';
            $str.='<downloadLink><![CDATA[http://down1.fam365.com'.$smxml->androiddown.']]></downloadLink>';
        }elseif ($smxml->pid == 2){
            $str.='<fileSize><![CDATA['.$smxml->ios_size.$gborm.']]></fileSize>';
            $str.='<version><![CDATA['.$smxml->ios_version.']]></version>';
            $str.='<os><![CDATA[ios]]></os>';
            $str.='<downloadLink><![CDATA['.$smxml->iosmarket.']]></downloadLink>';
        }
        $str.='<category><![CDATA['.$p[$smxml->classid][$smxml->classpid].']]></category>';
        $str.='<installation><![CDATA[1653]]></installation>';
        $str.='<tags><![CDATA['.$smxml->keyword.']]></tags>';
        $str.='</item>';
        $str.='</datalist>';
        return $str;
    }
}